# 高级特性

# 切片
from collections import Iterable, Iterator

ary = []
for i in range(100):
    ary.append(i + 1)

print(ary)
print(ary[:10])
print(ary[1:10:2])
print(ary[9::10])


# 掐头去尾空格
def trim(s):
    # 起始位置
    start = -1
    # 结束位置
    end = 1
    for idx in range(len(s)):
        if start == -1 and s[idx] != " ":
            start = idx
        if end == 1 and s[-idx - 1] != " ":
            end = -idx
        if start > -1 and end < 1:
            break
    return s[start:end] if end < 0 or (start == -1 and end == 1) else s[start:]


print(trim("      "))
print(isinstance("abc", Iterable))
print(isinstance(2, Iterable))


def getMinAndMax(L):
    if len(L) == 0:
        return (None, None)
    min = L[0]
    max = L[0]
    for i in L:
        if i >= max:
            max = i
        if i <= min:
            min = i
    return (min, max)


print(getMinAndMax([7, 1, 3, 9, 5]))


def toAry(obj):
    if not isinstance(obj, Iterable):
        raise TypeError(obj, "不可迭代")
    return [key + "=" + value for (key, value) in obj.items()]


print(toAry({
    "a": "1",
    "b": "2",
    "c": "3",
}))


# 列表生成式-数组转成小写
def lowercase(ary):
    return [val.lower() for val in L1 if isinstance(val, str)]


L1 = ['Hello', 'World', 18, 'Apple', None]
print(lowercase(L1))


# 生成器
def toItor(ary):
    return (val for val in ary)


print(isinstance(L1, Iterable))
print(isinstance(L1, Iterator))
L2 = toItor(L1)
for val in L2:
    print(val)

print(isinstance(L2, Iterable))
print(isinstance(L2, Iterator))

# 杨辉三角
